#!user/bin/env python3
# -- coding: utf-8 --
# @Createtime：2023/9/11 16:29
# @Updatetime：16:29
# @Author：Test008
# @File：file_define
# @Description：
"""
和文件相关的类定义
"""

# import json
# from data_define import Record
# class FileReader:
#     def read_data(self) -> list[Record]:
#         """
#         读取文件是的数据，读到的每一条数据都转换为Record对象，将它们都封装到list内返回
#         :return:
#         """
#         pass
# class TextFileReader(FileReader):
#
#     def __init__(self, path):
#         self.path = path
#
#     # 复写父类的方法
#     def read_data(self) -> list[Record]:
#         with open(self.path, "r", encoding="utf-8") as f:
#             record_list: list[Record] = []
#             for line in f.readlines():
#                 line = line.strip()  # 消除每行的\n
#                 data_list = line.split(",")  # 逗号分割
#                 record = Record(data_list[0], data_list[1], int(data_list[2]), data_list[3])
#                 record_list.append(record)
#
#         return record_list
# class JsonFileReader(FileReader):
#
#     def __init__(self, path):
#         self.path = path
#
#     def read_data(self) -> list[Record]:
#         with open(self.path, "r", encoding="UTF-8") as f:
#             record_list: list[Record] = []
#             for line in f.readlines():
#                 line = line.strip()
#                 data_list = json.loads(line)
#                 record = Record(data_list["date"], data_list["order_id"], int(data_list["money"]),
#                                 data_list["province"])
#                 record_list.append(record)
#         return record_list
# if __name__ == '__main__':
#     text_file_reader = TextFileReader("../../../static/doc/2011年1月销售数据.txt")
#     json_file_reader = JsonFileReader("../../../static/doc/2011年2月销售数据JSON.txt")
#     list1 = text_file_reader.read_data()
#     list2 = json_file_reader.read_data()
#     # for l in list1:
#     #     print(l)
#     for l in list2:
#         print(l)
import json

# import data_define
from data_define import Record

# class ReaderFile(object):
#
#     def read_data(self) -> Record:
#         pass
#
#
# class TextReadfile(ReaderFile):
#
#     def __init__(self, path):
#         self.path = path
#
#     def read_data(self) -> list[Record]:
#         with open(self.path, "r", encoding="UTF-8") as f:
#             content = f.readlines()
#             record_list: list[Record] = []
#             for line in content:
#                 line = line.strip()
#                 data_list = line.split(",")
#                 record = Record(data_list[0], data_list[1], int(data_list[2]), data_list[3])
#                 record_list.append(record)
#
#         return record_list
#
#
# class JsonReadfile(ReaderFile):
#
#     def __init__(self, path):
#         self.path = path
#
#     def read_data(self) -> list[Record]:
#         with open(self.path, "r", encoding="UTF-8") as f:
#             content = f.readlines()
#             record_list: list[Record] = []
#             for line in content:
#                 line = line.strip()
#                 data_list = json.loads(line)
#                 record = Record(data_list["date"], data_list["order_id"], int(data_list["money"]),
#                                 data_list["province"])
#                 record_list.append(record)
#
#         return record_list
#
#
# if __name__ == '__main__':
#     # text_file_read = TextReadfile("../../../static/doc/2011年1月销售数据.txt")
#     json_file_read = JsonReadfile("../../../static/doc/2011年2月销售数据JSON.txt")
#     # line1 = text_file_read.read_data()
#     line2 = json_file_read.read_data()
#     # for l in line1:
#     #     print(type(line1), l)
#     for l in line2:
#         print(l)

import data_define
from data_define import Record


class FileReader:

    def read_data(self) -> Record:
        pass


class TxtReader(FileReader):

    def __init__(self, path):
        self.path = path

    def read_data(self) -> list[Record]:
        with open(self.path, "r", encoding="UTF-8") as f:
            record_list: list = []
            for line in f.readlines():
                line = line.strip()
                data_list = line.split(",")
                record = Record(data_list[0], data_list[1], int(data_list[2]), data_list[3])
                record_list.append(record)

        return record_list


class JsonReader(FileReader):

    def __init__(self, path):
        self.path = path

    def read_data(self) -> list[Record]:
        with open(self.path, "r", encoding="UTF-8") as f:
            line = f.readlines()
            record_list = []
        for data in line:
            data_list = data.strip()
            data_list = json.loads(data_list)
            record = Record(data_list["date"], data_list["order_id"], int(data_list["money"]), data_list["province"])
            record_list.append(record)
        return record_list


if __name__ == '__main__':
    txt = TxtReader("../../../static/doc/2011年1月销售数据.txt")
    # txt.read_data()
    # for i1 in txt.read_data():
    #     print(i1)

    jsontext = JsonReader("../../../static/doc/2011年2月销售数据JSON.txt")
    for i2 in jsontext.read_data():
        print(i2)
